%--------------------------------------------------------------------------
%This program extracts the largest strongly connected set of establishments
%from the BeH. Note only establishments that hire from N are considered.
%--------------------------------------------------------------------------

clc;
clear all;

cd '\\employers\matlab\sconn_idfy';
addpath('\\employers\matlab\functions');


%-------------------------------------------------------------------------%
% create looping variables for sample windows
%-------------------------------------------------------------------------%


%declare custom windows 
vervec(1) = 31;
vervec(2) = 32;
c=3; 
for w = 3:4:7 
    for y = 1997:1:2019
        for s = 1:2
            if y + (w-1)/2 <2019 && y - (w-1)/2 >1997   
                ver = y*100 + w*10 + s;
                vervec(c) = ver;
                c = c+1;
            end
        end
    end
end
clear c;
vervec = vervec';

%-------------------------------------------------------------------------%
%loop over sample windows
%-------------------------------------------------------------------------%

for i = 1:length(vervec) 
    ver = vervec(i)
    
    %--------------------------------------------------------------------------
    % All moves dataset
    %--------------------------------------------------------------------------

    fnamein = ['flows_skill_ver',num2str(ver),'.csv'];
    fnameout = ['sconn_idfy_skill_ver',num2str(ver),'.csv'];
    clear ver

    all_moves = csvread(fnamein,1,0);
    %drop the row of flows
    all_moves = all_moves(:,1:2);

    %find the strongly connected set
    D = [all_moves ones(length(all_moves),1)];
    A = spconvert(D); %adjacency matrix
    clear D;
    [sci,sizes,rp,ci]=scomponents(A);
    clear A;
    [csize,cind] = max(sizes);
    sconn = sci==cind; %identify estabs in the largest strongly connected set
    %output the id's and indicators of belonging to the strongly connected set
    %to csv
    sconn = [(1:1:length(sconn))' sconn];
    dlmwrite(fnameout,sconn,'precision','%10.9g');
    
end